#include  <stddef.h>
#include <stdio.h>
#include  <stdlib.h>
#include "./readbitmap.h"
#include "defs.h"
typedef unsigned short      WORD;
typedef unsigned long       DWORD;
typedef long LONG;
#define BI_RGB        0L
typedef void *HANDLE;
#define GENERIC_WRITE                    (0x40000000L)
#define CREATE_ALWAYS       2
typedef int                 BOOL;
#define TRUE                1
#define FALSE                0

typedef struct tagBITMAPFILEHEADER {
        WORD    bfType;
        DWORD   bfSize;
        WORD    bfReserved1;
        WORD    bfReserved2;
        DWORD   bfOffBits;
} BITMAPFILEHEADER;

typedef struct tagBITMAPINFOHEADER{
        DWORD      biSize;
        LONG       biWidth;
        LONG       biHeight;
        WORD       biPlanes;
        WORD       biBitCount;
        DWORD      biCompression;
        DWORD      biSizeImage;
        LONG       biXPelsPerMeter;
        LONG       biYPelsPerMeter;
        DWORD      biClrUsed;
        DWORD      biClrImportant;
} BITMAPINFOHEADER;

void saveBmpFile(const char* fileName, unsigned char* pImgData, int width, int height)
{
	int imgLength = width * height * 4;
	BITMAPFILEHEADER bmheader;
	memset(&bmheader, 0, sizeof(bmheader));
	bmheader.bfType = 0x4d42;     //图像格式。必须为'BM'格式。  
	bmheader.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER); //从文件开头到数据的偏移量  
	bmheader.bfSize = imgLength + bmheader.bfOffBits;//文件大小  

	BITMAPINFOHEADER bmInfo;
	memset(&bmInfo, 0, sizeof(bmInfo));
	bmInfo.biSize = sizeof(bmInfo);
	bmInfo.biWidth = width;
	bmInfo.biHeight = height;
	bmInfo.biPlanes = 1;
	// bmInfo.biBitCount = 24;
	bmInfo.biBitCount = 32;
	bmInfo.biCompression = BI_RGB;

	FILE * hFile = fopen(fileName,"wb");
	if (hFile != NULL) {
		DWORD dwWritten;
        BOOL bRet = fwrite( &bmheader, sizeof(BITMAPFILEHEADER), 1, hFile);
		// assert(TRUE == bRet);
		bRet = fwrite(&bmInfo, sizeof(BITMAPINFOHEADER), 1, hFile);
		// assert(TRUE == bRet);
		bRet = fwrite(pImgData, sizeof(unsigned char), imgLength, hFile);
		// assert(TRUE == bRet);
		fclose(hFile);
	}
}

int test_save_bmp(){
    BmpInfo *bmp = readBmp("../data/dump_image/tmpfs/2272.bmp");
    char *dstFile="./tmp/test_save_bmp.bmp";
    int i = 0;
    for (i = 0; i < 16; i++) {
		printf("%d \n", bmp->rgbData[i]);
	}
    
    saveBmpFile(dstFile,bmp->rgbData,PIC_W,PIC_H);

    
}

void save_residual_data(char * path,int ***residual){
	FILE * hFile = fopen(path,"wb");
	int count=0;
	int i=0,k=0,j=0;
	for(k=0;k<17;k++){
		for(i=0;i<4;i++){
			for(j=0;j<4;j++){
				int8_t data=residual[k][i][j];
				int ret = fwrite(&data, sizeof(int8_t), 1, hFile);
				count+=ret;
			}
		}
	}
	printf("total write %d bytes to file %s\n",count,path);
}

void save_residual_zigzig_data(char * path,int **residual){
	FILE * hFile = fopen(path,"wb");
	int count=0;
	int i=0,k=0,j=0;
	// for ( k = 0; k < 40000; k++)
	// {
		/* code */
	
		for(i=0;i<Y_Space+UV_Space;i++){
			for(j=0;j<16;j++){
				if(0<i&&i<Y_Space&&j==0){continue; }
				int8_t data=residual[i][j];
				int ret = fwrite(&data, sizeof(int8_t), 1, hFile);
				count+=ret;
			}
		}
	// }
	printf("total write %d bytes to file %s\n",count,path);
	fclose(hFile);
}

void save_allInOneFile_residual_zigzig_data(FILE * hFile,int **residual){
	int count=0;
	int i=0,k=0,j=0;
	// for ( k = 0; k < 40000; k++)
	// {
		/* code */
	
		for(i=0;i<Y_Space+UV_Space;i++){
			for(j=0;j<16;j++){
				if(0<i&&i<Y_Space&&j==0){continue; }
				int8_t data=residual[i][j];
				int ret = fwrite(&data, sizeof(int8_t), 1, hFile);
				count+=ret;
			}
		}
	// }
	printf("add %d bytes to Totalfile\n",count);
}

// void read_residual_zigzig_data(char * path){
// 	FILE * hFile = fopen(path,"wb");
// }